package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.GridAccount;
import com.ruoyi.system.mapper.GridAccountMapper;
import com.ruoyi.system.service.IGridAccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 帐号管理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-21
 */
@Service
public class GridAccountServiceImpl implements IGridAccountService {
    private static final Logger log = LoggerFactory.getLogger(GridAccountServiceImpl.class);

    @Autowired
    private GridAccountMapper gridAccountMapper;

    /**
     * 查询帐号管理
     *
     * @param userId 帐号管理主键
     * @return 帐号管理
     */
    @Override
    public GridAccount selectGridAccountByUserId(Long userId) {
        return gridAccountMapper.selectGridAccountByUserId(userId);
    }

    /**
     * 查询帐号管理列表
     *
     * @param gridAccount 帐号管理
     * @return 帐号管理
     */
    @Override
    public List<GridAccount> selectGridAccountList(GridAccount gridAccount) {
        return gridAccountMapper.selectGridAccountList(gridAccount);
    }


    /**
     * 校验用户名称是否唯一
     *
     * @param hostIp   主机IP
     * @param userName 帐号
     * @return 结果
     */
    @Override
    public String checkAccountUnique(String hostIp, String userName) {
        HashMap map = new HashMap();
        map.put("hostIp", hostIp);
        map.put("userName", hostIp);
        int count = gridAccountMapper.checkAccountUnique(map);
        if (count > 0) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增帐号管理
     *
     * @param gridAccount 帐号管理
     * @return 结果
     */
    @Override
    public int insertGridAccount(GridAccount gridAccount) {
        gridAccount.setCreateTime(DateUtils.getNowDate());
        return gridAccountMapper.insertGridAccount(gridAccount);
    }

    /**
     * 修改用户状态
     *
     * @param gridAccount 用户信息
     * @return 结果
     */
    @Override
    public int updateGridPolicyStatus(GridAccount gridAccount) {
        return gridAccountMapper.updateGridAccount(gridAccount);
    }

    /**
     * 修改帐号管理
     *
     * @param gridAccount 帐号管理
     * @return 结果
     */
    @Override
    public int updateGridAccount(GridAccount gridAccount) {
        gridAccount.setUpdateTime(DateUtils.getNowDate());
        return gridAccountMapper.updateGridAccount(gridAccount);
    }

    /**
     * 批量删除帐号管理
     *
     * @param userIds 需要删除的帐号管理主键
     * @return 结果
     */
    @Override
    public int deleteGridAccountByUserIds(Long[] userIds) {
        return gridAccountMapper.deleteGridAccountByUserIds(userIds);
    }

    /**
     * 删除帐号管理信息
     *
     * @param userId 帐号管理主键
     * @return 结果
     */
    @Override
    public int deleteGridAccountByUserId(Long userId) {
        return gridAccountMapper.deleteGridAccountByUserId(userId);
    }


    /**
     * 导入用户数据
     *
     * @param userList        用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importGridAccount(List<GridAccount> userList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0) {
            throw new ServiceException("导入帐号数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String password = "";
        for (GridAccount user : userList) {
            try {
                HashMap map = new HashMap();
                map.put("hostIp", user.getHostIp());
                map.put("userName", user.getUserName());
                int count = gridAccountMapper.checkAccountUnique(map);
                if (count > 0) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getHostIp() + "-" + user.getUserName() + " 已存在");
                } else {
                    this.insertGridAccount(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getHostIp() + "-" + user.getUserName() + " 导入成功");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int getHostCount() {
        int count = gridAccountMapper.getHostCount();
        return count;
    }

    @Override
    public String getLastChangePassTime() {
        return gridAccountMapper.getLastChangePassTime();
    }

    @Override
    public int getChangePassCount() {
        return gridAccountMapper.getChangePassCount();
    }


    @Override
    public List<Map<String, String>> selectHostCatagory() {
        return gridAccountMapper.selectHostCatagory();
    }

    @Override
    public List<Map<String, String>> selectChangePassCatgoryList() {
        return gridAccountMapper.selectChangePassCatgoryList();
    }
}
